package com.aisile.admin.service.impl;

import com.aisile.admin.mapper.AdUserMapper;
import com.aisile.admin.service.AdChannelService;
import com.aisile.admin.service.AdUserService;
import com.aisile.model.admin.dtos.AdUserDto;
import com.aisile.model.admin.pojos.AdUser;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.aisile.utils.common.AppJwtUtil;
import com.aisile.utils.common.BCrypt;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mchange.v2.uid.UidUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.util.Date;
import java.util.HashMap;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper,AdUser> implements AdUserService {
    @Autowired
    private AdUserMapper adUserMapper;
    @Override
    public ResponseResult login(AdUserDto adUserDto) {
        if(adUserDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PATH_ERROR);
        }

        if(StringUtils.isEmpty(adUserDto.getName()) || StringUtils.isEmpty(adUserDto.getPassword())){
               return ResponseResult.errorResult(2002,"用户名或者密码不正确");
        }
        LambdaQueryWrapper<AdUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(adUserDto.getName()),AdUser::getName,adUserDto.getName());
        AdUser adUsers = adUserMapper.selectOne(wrapper);
        if(adUsers==null){
            return ResponseResult.errorResult(2001,"用户名不正确");
        }
        //判断状态
        if(adUsers.getStatus()!=9){
            return ResponseResult.errorResult(2003,"状态不对");
        }
        //用户行了，状态也行了，看看密码行不行

      if(!BCrypt.checkpw(adUserDto.getPassword(),adUsers.getPassword())){
          return ResponseResult.errorResult(2004,"密码不对");
      }
      //反向维护登录时间
        adUsers.setLoginTime(new Date());
      adUserMapper.updateById(adUsers);

//        LambdaQueryWrapper<AdUser> wrapper1=new LambdaQueryWrapper<>();
//        wrapper.like(!StringUtils.isEmpty(adUserDto.getPassword()),AdUser::getPassword,adUserDto.getPassword());
//        List<AdUser> adUsers1 = adUserMapper.selectList(wrapper1);
//        if(adUsers1==null){
//            return ResponseResult.errorResult(2001,"密码不正确");
//        }

        String token= AppJwtUtil.getToken(adUsers.getId().longValue());
        adUsers.setEmail("************");
        adUsers.setPhone("******************");
        adUsers.setPassword("**********");
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);

        map.put("user",adUsers);
        return ResponseResult.okResult(adUsers);
    }
}
